import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/login'
import Layout from '@/views/Layout'
import Home from '@/views/Home'
// import User from '@/views/User'
// import UserEditor from '@/views/User/UserEditor'
// import Search from '@/views/Search'
// import ArticleDetail from '@/views/ArticleDetail'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import Chat from '@/views/Chat'
import store from '@/store'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/layout' // 默认展示layout页面
},
{
  path: '/login',
  //   component: Login
  component: () => import(/* webpackChunkName: "login" */ '@/views/login'),
  beforeEnter: (to, from, next) => {
    // 需求:登陆状态下,想要去/login路由,留在原地,不让去
    if (store.state.token.length > 0) {
      next(false)// 留在原地,不让去
    } else {
      next() // 其他情况可以
    }
  }
},
{
  path: '/layout',
  component: Layout,
  redirect: '/layout/home', // 进入layout组件马上跳转二级路由显示home
  children: [
    {
      path: 'home',
      component: Home,
      meta: { // 路由信息对象里设置参数,保留滚动条位置
        isScoll: true, // 这个页面是都需要滚动条,在路由守卫中判断
        top: 0 // 离开次路由页面时,滚动条的位置

      }
    },
    {
      path: 'user',
      //   component: User
      component: () => import(/* webpackChunkName: "User" */ '@/views/User')
    }
  ]
},
{
  path: '/search',
  //   component: Search
  component: () => import(/* webpackChunkName: "Search" */ '@/views/Search')
},
{
  path: '/search/:keywords',
  //   component: SearchResult
  component: () => import(/* webpackChunkName: "SearchResult" */ '@/views/Search/SearchResult.vue')
},
{
  path: '/articleDetail',
  //   component: ArticleDetail
  component: () => import(/* webpackChunkName: "ArticleDetail" */ '@/views/ArticleDetail')
},
{
  path: '/userEditor',
  //   component: UserEditor
  component: () => import(/* webpackChunkName: "UserEditor" */ '@/views/User/UserEditor')
},
{
  path: '/chat',
  //   component: Chat
  component: () => import(/* webpackChunkName: "Chat" */ '@/views/Chat')
}
]

const router = new VueRouter({
  routes
})

// 全局的前置守卫
// router.beforeEach((to, from, next) => {
// // 需求:登陆状态下,想要去/login路由,留在原地,不让去
//   if (store.state.token.length > 0 && to.path === '/login') {
//     next(false)// 留在原地,不让去
//   } else {
//     next() // 其他情况可以
//   }
// })
// token过期
// 如果有token,但是需要验证身份的功能(后端接口:点赞/评论/反馈)=>过期的token带给后台
// axios响应拦截器=>判断token过期=>清除token=>然后跳转路由=>然后由路由全局前置守卫进入判断=>没有token放行到登陆页面

export default router
